package com.happyscan138.makefun.Fragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.PopupWindow;

import com.happyscan138.makefun.BaseAdapter.MyCollectionvideo;
import com.happyscan138.makefun.R;
import com.happyscan138.makefun.Urls.Urls;
import com.happyscan138.makefun.VideoDetail;
import com.happyscan138.makefun.adapter.MyCollectionVideoAdapter;
import com.happyscan138.makefun.base.LoadListView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

import static android.content.Context.MODE_PRIVATE;
import static com.happyscan138.makefun.Urls.Urls.base;
import static com.happyscan138.makefun.base.http.client;
import static com.happyscan138.makefun.base.http.params;
import static com.loopj.android.http.AsyncHttpClient.log;

/**
 * Created by Administrator on 2017/5/28.
 */

public class MyCollectionVideo extends Fragment implements LoadListView.IloadListener {
    private List<MyCollectionvideo> ListData;
    private LoadListView listView_myDiscuss_question;
    private SharedPreferences preferences;
    private String like_type_id;
    private PopupWindow pop;
    private Intent intent;
    private MyCollectionvideo data;
    private int LIKE_ID;
    private Handler handler;
    private ArrayList<Integer> ID;
    private String access_key;
    private int  PROBLEM_ID_INDEX ;
    private MyCollectionVideoAdapter adapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.e("mycollection","mycollectiongragment");
        View view=inflater.inflate(R.layout.persional_video,null);
        client=new AsyncHttpClient();
        params=new RequestParams();
        preferences = getActivity().getSharedPreferences("UserInfo", MODE_PRIVATE);
        access_key=preferences.getString("UserId","");
        like_type_id = preferences.getString("LikeTypeID", "");
        initView(view);
        getAdapterView(view);
        listView_myDiscuss_question.setInterface(this);
        return view;
    }
    public void initView(View view){
        listView_myDiscuss_question=(LoadListView)view.findViewById(R.id.persional_video);
    }
    public View getAdapterView(View view) {
        log.e("discusscideo","discusscideo");
        ListData = new ArrayList<>();
        ID=new ArrayList<Integer>();
        params.put("access_key", access_key);
        params.put("like_type_id", like_type_id);
        params.put("page_count", 10);
        params.put("like_id_index", 0);

        client.post(Urls.MyCollectionVideo,params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                String jsonStr=response.toString();

                try {
                    JSONObject jsonObject=new JSONObject(jsonStr);
                    JSONArray jsonArray=jsonObject.getJSONArray("data");
                    for (int i=0;i<jsonArray.length();i++){
                        JSONObject objectData=jsonArray.getJSONObject(i);
                        PROBLEM_ID_INDEX = objectData.getInt("like_id");
                        ID.add(PROBLEM_ID_INDEX);
                        data=new MyCollectionvideo();
                        data.head=base+objectData.getString("img_path");

                        data.name=objectData.getString("nick_name");
                        data.resource= base+objectData.getString("resource");
                        data.text=objectData.getString("content");
                        data.problem_id=objectData.getString("problem_id");
                        data.like_id = objectData.getString("like_id");
                        ListData.add(data);

                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                adapter=new MyCollectionVideoAdapter(getActivity(), ListData,access_key);
                listView_myDiscuss_question.setAdapter(adapter);
                listView_myDiscuss_question.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                        intent = new Intent();
                        data = ListData.get(position);
                        intent.putExtra("problem_id", data.problem_id);
                        log.e("problem_id",data.problem_id);
                        intent.setClass(getActivity(), VideoDetail.class);
                        startActivity(intent);

                    }
                });

            }


        });

        return view;
    }

    @Override
    public void onLoad() {
        handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {

                params.put("access_key", access_key);
                params.put("like_type_id", like_type_id);
                params.put("page_count", 10);
                params.put("like_id_index", ID.get(ID.size() - 1));
                client.post(Urls.MyCollectionVideo, params, new JsonHttpResponseHandler() {
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                        super.onSuccess(statusCode, headers, response);
                        String jsonStr = response.toString();
                        log.e("discusscideojsonStr", jsonStr);
                        try {
                            JSONObject jsonObject = new JSONObject(jsonStr);
                            JSONArray jsonArray = jsonObject.getJSONArray("data");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject objectData = jsonArray.getJSONObject(i);
                                PROBLEM_ID_INDEX = objectData.getInt("like_id");
                                ID.add(PROBLEM_ID_INDEX);
                                data = new MyCollectionvideo();
                                data.head = base + objectData.getString("img_path");

                                data.name = objectData.getString("nick_name");
                                data.resource = base + objectData.getString("resource");
                                data.text = objectData.getString("content");
                                data.problem_id = objectData.getString("problem_id");
                                ListData.add(data);

                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        adapter.notifyDataSetChanged();
                    }
                });
                listView_myDiscuss_question.loadComplete();
            }
        }, 1000);
    }
}
